package com.example.chatty.screens.conversation

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.compose.rememberNavController
import com.example.chatty.R
import com.example.chatty.logic.model.AppScreen
import com.example.chatty.logic.model.ConversationUiState
import com.example.chatty.logic.model.Message
import com.example.chatty.logic.model.UserProfileData
import com.example.chatty.logic.utils.LocalNavController
import com.example.chatty.screens.conversation.mock.initialMessages
import com.example.chatty.screens.home.mock.fetchUserInfoById
import com.example.chatty.ui.IMChat.Messages
import com.example.chatty.ui.theme.chattyColors
import kotlinx.coroutines.launch

val LocalConversationUser = compositionLocalOf<UserProfileData> {
    error("CompositionLocal LocalConversationUser not found")
}

@Preview
@Composable
fun ConversationScreen(
    uiState: ConversationUiState = ConversationUiState(
        initialMessages = initialMessages,
        conversationUserId = "1024"
    ),
    modifier: Modifier = Modifier
) {

    val timeNow = stringResource(id = R.string.now)
    val scope = rememberCoroutineScope()
    val scrollState = rememberLazyListState()


    Surface(modifier = modifier) {
        Box(modifier = Modifier.fillMaxSize()) {

            val navController =
                if (LocalInspectionMode.current) rememberNavController() else LocalNavController.current

            Column(
                Modifier
                    .fillMaxSize()
                    .background(MaterialTheme.chattyColors.backgroundColor)
            ) {
                CompositionLocalProvider(LocalConversationUser provides fetchUserInfoById(uiState.conversationUserId)) {
                    Messages(messages = uiState.messages, navigateToProfile = {
                        navController.navigate(
                            "${AppScreen.userProfile}/${uiState.conversationUserId}"
                        )
                    }, modifier = Modifier.weight(1f), scrollState = scrollState)
                }
                UserInput(
                    onMessageSent = { content ->
                        uiState.addMessage(
                            Message(
                                true,
                                content,
                                timeNow
                            )
                        )
                    }, resetScroll = {
                        scope.launch {
                            scrollState.scrollToItem(0)
                        }
                    }, modifier = Modifier
                        .navigationBarsPadding()
                        .imePadding()

                )

            }

            ConversationTopBar(
                conversationName = fetchUserInfoById(uiState.conversationUserId).nickname,
                onNavIconPressed = { navController.popBackStack() },
                modifier = Modifier
            )
        }
    }
}






